//
//  SettingTimeViewController.m
//  TaiHao
//
//  Created by rzm on 16/6/1.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxInstallTimeVC.h"
#import "SampuxInstallTimeDetailVC.h"
#import "SPTimeValue.h"
#import "SampuxGetTimeValue.h"

@interface SampuxInstallTimeVC ()<TimeSettingSelectedRowDelegate>

@property (nonatomic, strong)  NSMutableArray *tempArr ;
@property (nonatomic, assign)  BOOL needRefresh ;

@end

@implementation SampuxInstallTimeVC

- (void)viewDidLoad {
    [super viewDidLoad];          
    [self.navigationItem setHidesBackButton:YES];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(requestTimeProgram:) name:NOTIFICATION_RESPONSE_DEVICE_PROGRAM_13001 object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(refreshData) name:@"setTimeSuccess" object:nil];
//    [GBInterMaster_CommForDeivce requestShowProgramInfo:0 withDeviceAccount:self.model.deviceAccount];
    [self createTimeView];
}

- (void)createTimeView
{
    _timeView = [[SampuxInstallTimeView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
    _timeView.timeDelegate = self;
    _timeView.listModel = self.listModel;

//    _timeView.model = self.configModel;
    _timeView.backgroundColor = THSystemClearColor;
    [self.view addSubview:_timeView];
}

- (void)refreshData
{
    self.needRefresh = YES;
}


- (void)timeSettingSelectedCellSub:(NSInteger)selectedCellSubTag andSubData:(SampuxInstallTimeModel *)model
{
    SampuxInstallTimeDetailVC *detailVC = [[SampuxInstallTimeDetailVC alloc]init];
    detailVC.listModel = self.listModel;
    detailVC.reciveModelFromVC = model;
//    detailVC.tempArr = LimitTempArr([model.lowValue intValue], [model.topValue intValue]);
    
    detailVC.tempArr = [SampuxGlobalTool createArrWithMin:[model.lowValue longLongValue] andMax:[model.topValue longLongValue]];
//    detailVC.configModel = self.configModel;
    detailVC.title = self.title;
    detailVC.markTag = selectedCellSubTag;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    SampuxCustomNavController *nav = (SampuxCustomNavController *)self.navigationController;
    nav.titleStr = @"定时设置";
    nav.rightBtn.hidden = NO;
    if (self.needRefresh) {
        [self.timeView.header beginRefreshing];
    }
}

- (void)setListModel:(SampuxListModel *)listModel
{
    _listModel = listModel;
}

- (void)requestTimeProgram:(NSNotification *)notification
{
    id userInfo = [notification userInfo];
    if(userInfo == nil){
        return;
    }
    NSDictionary * result = userInfo;
    int msgNumber = [[result objectForKey:@"msgNumber"] intValue];
    int responseCode = [[result objectForKey:@"responseCode"] intValue];
    NSLog(@"requestTimeProgram == 结果，消息序号：%d，响应码：%x",msgNumber,responseCode);
    _tempArr = [NSMutableArray array];

    if(responseCode == 0x00){
        [_tempArr addObjectsFromArray:[result objectForKey:@"programs"]];
        NSArray *arr = [result objectForKey:@"programs"];
        [self obtainTimeData];
    }else
    {
            [SVProgressHUD showErrorWithStatus:@"获取时段信息失败"];
//        [MBProgressHUD showError:@"获取时段信息失败"];
    }
}


- (void)obtainTimeData
{
    [self.timeView.modelArr removeAllObjects];
    NSMutableArray *array = [NSMutableArray array];
    array = [SampuxGetTimeValue getTimeData:_tempArr];
    [self.timeView.modelArr addObjectsFromArray:array];
    self.timeView.modelArr = self.timeView.modelArr;
//    SampuxInstallTimeModel *model = array[0];
//    self.configModel.limitedArr = LimitTempArr([model.topValue intValue], [model.lowValue intValue]);

}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
